import ctypes as c


# As of ctypes 1.0, ctypes does not support custom error-checking
# functions on callbacks, nor does it support custom datatypes on
# callbacks, so we must ensure that all callbacks return
# primitive datatypes.
#
# Non-primitive return values wrapped with UNCHECKED won't be
# typechecked, and will be converted to ctypes.c_void_p.
def UNCHECKED(type):
    if hasattr(type, "_type_") and isinstance(type._type_, str) and type._type_ != "P":
        return type
    else:
        return c.c_void_p


VTERM_COLOR_INDEXED = 0x01# D:/opt/msys64/ucrt64/include/vterm.h: 103
VTERM_COLOR_TYPE_MASK = 0x01# D:/opt/msys64/ucrt64/include/vterm.h: 103
VTERM_COLOR_DEFAULT_FG = 0x02# D:/opt/msys64/ucrt64/include/vterm.h: 103
VTERM_COLOR_DEFAULT_BG = 0x04# D:/opt/msys64/ucrt64/include/vterm.h: 103
VTERM_COLOR_DEFAULT_MASK = 0x06# D:/opt/msys64/ucrt64/include/vterm.h: 103

VTERM_ATTR_BOLD = 1# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_UNDERLINE = (VTERM_ATTR_BOLD + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_ITALIC = (VTERM_ATTR_UNDERLINE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_BLINK = (VTERM_ATTR_ITALIC + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_REVERSE = (VTERM_ATTR_BLINK + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_CONCEAL = (VTERM_ATTR_REVERSE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_STRIKE = (VTERM_ATTR_CONCEAL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_FONT = (VTERM_ATTR_STRIKE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_FOREGROUND = (VTERM_ATTR_FONT + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_BACKGROUND = (VTERM_ATTR_FOREGROUND + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_SMALL = (VTERM_ATTR_BACKGROUND + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_ATTR_BASELINE = (VTERM_ATTR_SMALL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250
VTERM_N_ATTRS = (VTERM_ATTR_BASELINE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 250

VTERM_PROP_CURSORVISIBLE = 1# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_CURSORBLINK = (VTERM_PROP_CURSORVISIBLE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_ALTSCREEN = (VTERM_PROP_CURSORBLINK + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_TITLE = (VTERM_PROP_ALTSCREEN + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_ICONNAME = (VTERM_PROP_TITLE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_REVERSE = (VTERM_PROP_ICONNAME + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_CURSORSHAPE = (VTERM_PROP_REVERSE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_MOUSE = (VTERM_PROP_CURSORSHAPE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_PROP_FOCUSREPORT = (VTERM_PROP_MOUSE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265
VTERM_N_PROPS = (VTERM_PROP_FOCUSREPORT + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 265

VTERM_PROP_CURSORSHAPE_BLOCK = 1# D:/opt/msys64/ucrt64/include/vterm.h: 267
VTERM_PROP_CURSORSHAPE_UNDERLINE = (VTERM_PROP_CURSORSHAPE_BLOCK + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 267
VTERM_PROP_CURSORSHAPE_BAR_LEFT = (VTERM_PROP_CURSORSHAPE_UNDERLINE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 267
VTERM_N_PROP_CURSORSHAPES = (VTERM_PROP_CURSORSHAPE_BAR_LEFT + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 267

VTERM_UNDERLINE_OFF = 0# D:/opt/msys64/ucrt64/include/vterm.h: 514
VTERM_UNDERLINE_SINGLE = (VTERM_UNDERLINE_OFF + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 514
VTERM_UNDERLINE_DOUBLE = (VTERM_UNDERLINE_SINGLE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 514
VTERM_UNDERLINE_CURLY = (VTERM_UNDERLINE_DOUBLE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 514

VTERM_BASELINE_NORMAL = 0# D:/opt/msys64/ucrt64/include/vterm.h: 521
VTERM_BASELINE_RAISE = (VTERM_BASELINE_NORMAL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 521
VTERM_BASELINE_LOWER = (VTERM_BASELINE_RAISE + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 521

VTERM_DAMAGE_CELL = 0# D:/opt/msys64/ucrt64/include/vterm.h: 568
VTERM_DAMAGE_ROW = (VTERM_DAMAGE_CELL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 568
VTERM_DAMAGE_SCREEN = (VTERM_DAMAGE_ROW + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 568
VTERM_DAMAGE_SCROLL = (VTERM_DAMAGE_SCREEN + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 568
VTERM_N_DAMAGES = (VTERM_DAMAGE_SCROLL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 568

VTERM_ATTR_BOLD_MASK = (1 << 0)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_UNDERLINE_MASK = (1 << 1)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_ITALIC_MASK = (1 << 2)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_BLINK_MASK = (1 << 3)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_REVERSE_MASK = (1 << 4)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_STRIKE_MASK = (1 << 5)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_FONT_MASK = (1 << 6)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_FOREGROUND_MASK = (1 << 7)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_BACKGROUND_MASK = (1 << 8)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_CONCEAL_MASK = (1 << 9)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_SMALL_MASK = (1 << 10)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ATTR_BASELINE_MASK = (1 << 11)# D:/opt/msys64/ucrt64/include/vterm.h: 594
VTERM_ALL_ATTRS_MASK = ((1 << 12) - 1)# D:/opt/msys64/ucrt64/include/vterm.h: 594

VTERM_VALUETYPE_BOOL = 1# D:/opt/msys64/ucrt64/include/vterm.h: 218
VTERM_VALUETYPE_INT = (VTERM_VALUETYPE_BOOL + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 218
VTERM_VALUETYPE_STRING = (VTERM_VALUETYPE_INT + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 218
VTERM_VALUETYPE_COLOR = (VTERM_VALUETYPE_STRING + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 218
VTERM_N_VALUETYPES = (VTERM_VALUETYPE_COLOR + 1)# D:/opt/msys64/ucrt64/include/vterm.h: 218

uint8_t = c.c_ubyte # D:/opt/msys64/ucrt64/include/stdint.h: 36
uint32_t = c.c_uint # D:/opt/msys64/ucrt64/include/stdint.h: 40
VTermModifier = c.c_int # D:/opt/msys64/ucrt64/include/vterm_keycodes.h: 11
VTermKey = c.c_int #D:/opt/msys64/ucrt64/include/vterm_keycodes.h: 57
VTermAttr = c.c_int
VTermColorType = c.c_int
VTermProp = c.c_int
VTermSelectionMask = c.c_int
VTermDamageSize = c.c_int
VTermAttrMask = c.c_int
VTermValueType = c.c_int


VTermOutputCallback = c.CFUNCTYPE(UNCHECKED(None), c.c_char_p, c.c_size_t, c.POINTER(None))# D:/opt/msys64/ucrt64/include/vterm.h: 356


class VTerm(c.Structure):
    pass


class VTermScreen(c.Structure):
    pass


class VTermState(c.Structure):
    pass


class VTermPos(c.Structure):
    _fields_ = [("row", c.c_int),
                ("col", c.c_int)]


class VTermRect(c.Structure):
    _fields_ = [("start_row", c.c_int),
                ("end_row", c.c_int),
                ("start_col", c.c_int),
                ("end_col", c.c_int)]


class VTermColorRGB(c.Structure):
    _fields_ = [("type", uint8_t),
                ("red", uint8_t),
                ("green", uint8_t),
                ("blue", uint8_t)]


class VTermColorIndexed(c.Structure):
    _fields_ = [("type", uint8_t),
                ("idx", uint8_t)]


class VTermColor(c.Union):
    _fields_ = [("type", uint8_t),
                ("rgb", VTermColorRGB),
                ("indexed", VTermColorIndexed)]


class VTermGlyphInfo(c.Structure):
    _fields_ = [
        ('chars', c.POINTER(uint32_t)),
        ('width', c.c_int),
        ('protected_cell', c.c_uint, 1),
        ('dwl', c.c_uint, 1),
        ('dhl', c.c_uint, 2),
    ]


class VTermLineInfo(c.Structure):
    _fields_ = [
        ('doublewidth', c.c_uint, 1),
        ('doubleheight', c.c_uint, 2),
        ('continuation', c.c_uint, 1),
    ]


class VTermStateFields(c.Structure):
    _fields_ = [
        ('pos', VTermPos),
        ('lineinfos', c.POINTER(VTermLineInfo) * int(2)),
    ]


class VTermAllocatorFunctions(c.Structure):
    _fields_ = [
        ('malloc', c.CFUNCTYPE(UNCHECKED(c.POINTER(c.c_ubyte)), c.c_size_t, c.POINTER(None))),
        ('free', c.CFUNCTYPE(UNCHECKED(None), c.POINTER(None), c.POINTER(None))),
    ]


class VTermBuilder(c.Structure):
    _fields_ = [
        ('ver', c.c_int),
        ('rows', c.c_int),
        ('cols', c.c_int),
        ('allocator', c.POINTER(VTermAllocatorFunctions)),
        ('allocdata', c.POINTER(None)),
        ('outbuffer_len', c.c_size_t),
        ('tmpbuffer_len', c.c_size_t),
    ]


class VTermStringFragment(c.Structure):
    _fields_ = [
        ('str', c.c_char_p),
        ('len', c.c_size_t, 30),
        ('initial', c.c_bool, 1),
        ('final', c.c_bool, 1),
    ]


class VTermParserCallbacks(c.Structure):
    _fields_ = [
        ('text', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.c_size_t, c.POINTER(None))),
        ('control', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_ubyte, c.POINTER(None))),
        ('escape', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.c_size_t, c.POINTER(None))),
        ('csi', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.POINTER(c.c_long), c.c_int, c.c_char_p, c.c_char, c.POINTER(None))),
        ('osc', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, VTermStringFragment, c.POINTER(None))),
        ('dcs', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.c_size_t, VTermStringFragment, c.POINTER(None))),
        ('apc', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
        ('pm', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
        ('sos', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
        ('resize', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.c_int, c.POINTER(None))),
    ]


class VTermValue(c.Union):
    _fields_ = [
        ('boolean', c.c_int),
        ('number', c.c_int),
        ('string', VTermStringFragment),
        ('color', VTermColor),
    ]


class VTermStateCallbacks(c.Structure):
    _fields_ = [
        ('putglyph', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(VTermGlyphInfo), VTermPos, c.POINTER(None))),
        ('movecursor', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermPos, VTermPos, c.c_int, c.POINTER(None))),
        ('scrollrect', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, c.c_int, c.c_int, c.POINTER(None))),
        ('moverect', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, VTermRect, c.POINTER(None))),
        ('erase', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, c.c_int, c.POINTER(None))),
        ('initpen', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(None))),
        ('setpenattr', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermAttr, c.POINTER(VTermValue), c.POINTER(None))),
        ('settermprop', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermProp, c.POINTER(VTermValue), c.POINTER(None))),
        ('bell', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(None))),
        ('resize', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.c_int, c.POINTER(VTermStateFields), c.POINTER(None))),
        ('setlineinfo', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.POINTER(VTermLineInfo), c.POINTER(VTermLineInfo), c.POINTER(None))),
        ('sb_clear', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(None))),
    ]


class VTermStateFallbacks(c.Structure):
    _fields_ = [
        ('control', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_ubyte, c.POINTER(None))),
        ('csi', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.POINTER(c.c_long), c.c_int, c.c_char_p, c.c_char, c.POINTER(None))),
        ('osc', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, VTermStringFragment, c.POINTER(None))),
        ('dcs', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_char_p, c.c_size_t, VTermStringFragment, c.POINTER(None))),
        ('apc', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
        ('pm', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
        ('sos', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermStringFragment, c.POINTER(None))),
    ]


class VTermSelectionCallbacks(c.Structure):
    _fields_ = [
        ('set', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermSelectionMask, VTermStringFragment, c.POINTER(None))),
        ('query', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermSelectionMask, c.POINTER(None))),
    ]


class VTermScreenCellAttrs(c.Structure):
    _fields_ = [
        ('bold', c.c_uint, 1),
        ('underline', c.c_uint, 2),
        ('italic', c.c_uint, 1),
        ('blink', c.c_uint, 1),
        ('reverse', c.c_uint, 1),
        ('conceal', c.c_uint, 1),
        ('strike', c.c_uint, 1),
        ('font', c.c_uint, 4),
        ('dwl', c.c_uint, 1),
        ('dhl', c.c_uint, 2),
        ('small', c.c_uint, 1),
        ('baseline', c.c_uint, 2),
    ]


class VTermScreenCell(c.Structure):
    _fields_ = [
        ('chars', uint32_t * int(6)),
        ('width', c.c_char),
        ('attrs', VTermScreenCellAttrs),
        ('fg', VTermColor),
        ('bg', VTermColor),
    ]


class VTermScreenCallbacks(c.Structure):
    _fields_ = [
        ('damage', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, c.POINTER(None))),
        ('moverect', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, VTermRect, c.POINTER(None))),
        ('movecursor', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermPos, VTermPos, c.c_int, c.POINTER(None))),
        ('settermprop', c.CFUNCTYPE(UNCHECKED(c.c_int), VTermProp, c.POINTER(VTermValue), c.POINTER(None))),
        ('bell', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(None))),
        ('resize', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.c_int, c.POINTER(None))),
        ('sb_pushline', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.POINTER(VTermScreenCell), c.POINTER(None))),
        ('sb_popline', c.CFUNCTYPE(UNCHECKED(c.c_int), c.c_int, c.POINTER(VTermScreenCell), c.POINTER(None))),
        ('sb_clear', c.CFUNCTYPE(UNCHECKED(c.c_int), c.POINTER(None))),
    ]


def load_vterm(path: str):
    _handle = c.CDLL(path)

    _handle.vterm_build.restype = c.POINTER(VTerm)
    _handle.vterm_build.argtypes = (c.POINTER(VTermBuilder),)

    _handle.vterm_new.restype = c.POINTER(VTerm)
    _handle.vterm_new.argtypes = (c.c_int, c.c_int)

    _handle.vterm_free.restype = None
    _handle.vterm_free.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_get_size.restype = None
    _handle.vterm_get_size.argtypes = (c.POINTER(VTerm), c.POINTER(c.c_int), c.POINTER(c.c_int))

    _handle.vterm_set_size.restype = None
    _handle.vterm_set_size.argtypes = (c.POINTER(VTerm), c.c_int, c.c_int)

    _handle.vterm_get_utf8.restype = c.c_int
    _handle.vterm_get_utf8.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_set_utf8.restype = None
    _handle.vterm_set_utf8.argtypes = (c.POINTER(VTerm), c.c_int)

    _handle.vterm_input_write.restype = c.c_size_t
    _handle.vterm_input_write.argtypes = (c.POINTER(VTerm), c.c_char_p, c.c_size_t)

    _handle.vterm_output_set_callback.restype = None
    _handle.vterm_output_set_callback.argtypes = (c.POINTER(VTerm), c.POINTER(VTermOutputCallback), c.POINTER(None))

    _handle.vterm_output_get_buffer_size.restype = c.c_size_t
    _handle.vterm_output_get_buffer_size.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_output_get_buffer_current.restype = c.c_size_t
    _handle.vterm_output_get_buffer_current.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_output_get_buffer_remaining.restype = c.c_size_t
    _handle.vterm_output_get_buffer_remaining.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_output_read.restype = c.c_size_t
    _handle.vterm_output_read.argtypes = (c.POINTER(VTerm), c.c_char_p, c.c_size_t)

    _handle.vterm_keyboard_unichar.restype = None
    _handle.vterm_keyboard_unichar.argtypes = (c.POINTER(VTerm), uint32_t, VTermModifier)

    _handle.vterm_keyboard_key.restype = None
    _handle.vterm_keyboard_key.argtypes = (c.POINTER(VTerm), VTermKey, VTermModifier)

    _handle.vterm_keyboard_start_paste.restype = None
    _handle.vterm_keyboard_start_paste.argtypes = (c.POINTER(VTerm), )

    _handle.vterm_keyboard_end_paste.restype = None
    _handle.vterm_keyboard_end_paste.argtypes = (c.POINTER(VTerm), )

    _handle.vterm_mouse_move.restype = None
    _handle.vterm_mouse_move.argtypes = (c.POINTER(VTerm), c.c_int, c.c_int, VTermModifier)

    _handle.vterm_mouse_button.restype = None
    _handle.vterm_mouse_button.argtypes = (c.POINTER(VTerm), c.c_int, c.c_bool, VTermModifier)

    _handle.vterm_parser_set_callbacks.restype = None
    _handle.vterm_parser_set_callbacks.argtypes = (c.POINTER(VTerm), c.POINTER(VTermParserCallbacks), c.POINTER(None))

    _handle.vterm_parser_get_cbdata.restype = c.POINTER(c.c_ubyte)
    _handle.vterm_parser_get_cbdata.argtypes = (c.POINTER(VTerm),)
    _handle.vterm_parser_get_cbdata.errcheck = lambda v,*a : c.cast(v, c.c_void_p)

    _handle.vterm_parser_set_emit_nul.restype = None
    _handle.vterm_parser_set_emit_nul.argtypes = (c.POINTER(VTerm), c.c_bool)

    _handle.vterm_obtain_state.restype = c.POINTER(VTermState)
    _handle.vterm_obtain_state.argtypes = (c.POINTER(VTerm), )

    _handle.vterm_state_set_callbacks.restype = None
    _handle.vterm_state_set_callbacks.argtypes = (c.POINTER(VTermState), c.POINTER(VTermStateCallbacks), c.POINTER(None) )

    _handle.vterm_state_get_cbdata.restype = c.POINTER(c.c_ubyte)
    _handle.vterm_state_get_cbdata.argtypes = (c.POINTER(VTermState), )
    _handle.vterm_state_get_cbdata.errcheck = lambda v,*a : c.cast(v, c.c_void_p)

    _handle.vterm_state_set_unrecognised_fallbacks.restype = None
    _handle.vterm_state_set_unrecognised_fallbacks.argtypes = (c.POINTER(VTermState), c.POINTER(VTermStateFallbacks), c.POINTER(None) )

    _handle.vterm_state_get_unrecognised_fbdata.restype = c.POINTER(c.c_ubyte)
    _handle.vterm_state_get_unrecognised_fbdata.argtypes = (c.POINTER(VTermState), )
    _handle.vterm_state_get_unrecognised_fbdata.errcheck = lambda v,*a : c.cast(v, c.c_void_p)

    _handle.vterm_state_reset.restype = None
    _handle.vterm_state_reset.argtypes = (c.POINTER(VTermState), c.c_int)

    _handle.vterm_state_get_cursorpos.restype = None
    _handle.vterm_state_get_cursorpos.argtypes = (c.POINTER(VTermState), c.POINTER(VTermPos))

    _handle.vterm_state_get_default_colors.restype = None
    _handle.vterm_state_get_default_colors.argtypes = (c.POINTER(VTermState), c.POINTER(VTermColor), c.POINTER(VTermColor))

    _handle.vterm_state_get_palette_color.restype = None
    _handle.vterm_state_get_palette_color.argtypes = (c.POINTER(VTermState), c.c_int, c.POINTER(VTermColor))

    _handle.vterm_state_set_default_colors.restype = None
    _handle.vterm_state_set_default_colors.argtypes = (c.POINTER(VTermState), c.POINTER(VTermColor), c.POINTER(VTermColor))

    _handle.vterm_state_set_palette_color.restype = None
    _handle.vterm_state_set_palette_color.argtypes = (c.POINTER(VTermState), c.c_int, c.POINTER(VTermColor))

    _handle.vterm_state_set_bold_highbright.restype = None
    _handle.vterm_state_set_bold_highbright.argtypes = (c.POINTER(VTermState), c.c_int)

    _handle.vterm_state_get_penattr.restype = c.c_int
    _handle.vterm_state_get_penattr.argtypes = (c.POINTER(VTermState), VTermAttr, c.POINTER(VTermValue))

    _handle.vterm_state_set_termprop.restype = c.c_int
    _handle.vterm_state_set_termprop.argtypes = (c.POINTER(VTermState), VTermProp, c.POINTER(VTermValue))

    _handle.vterm_state_focus_in.restype = None
    _handle.vterm_state_focus_in.argtypes = (c.POINTER(VTermState),)

    _handle.vterm_state_focus_out.restype = None
    _handle.vterm_state_focus_out.argtypes = (c.POINTER(VTermState),)

    _handle.vterm_state_get_lineinfo.restype = c.POINTER(VTermLineInfo)
    _handle.vterm_state_get_lineinfo.argtypes = (c.POINTER(VTermState), c.c_int)

    _handle.vterm_state_convert_color_to_rgb.restype = None
    _handle.vterm_state_convert_color_to_rgb.argtypes = (c.POINTER(VTermState), c.POINTER(VTermColor))

    _handle.vterm_state_set_selection_callbacks.restype = None
    _handle.vterm_state_set_selection_callbacks.argtypes = (c.POINTER(VTermState), c.POINTER(VTermSelectionCallbacks), c.POINTER(None), c.c_char_p, c.c_size_t)

    _handle.vterm_state_send_selection.restype = None
    _handle.vterm_state_send_selection.argtypes = (c.POINTER(VTermState), VTermSelectionMask, VTermStringFragment)

    _handle.vterm_obtain_screen.restype = c.POINTER(VTermScreen)
    _handle.vterm_obtain_screen.argtypes = (c.POINTER(VTerm),)

    _handle.vterm_screen_set_callbacks.restype = None
    _handle.vterm_screen_set_callbacks.argtypes = (c.POINTER(VTermScreen), c.POINTER(VTermScreenCallbacks), c.POINTER(None))

    _handle.vterm_screen_get_cbdata.restype = c.POINTER(c.c_ubyte)
    _handle.vterm_screen_get_cbdata.argtypes = (c.POINTER(VTermScreen),)

    _handle.vterm_screen_set_unrecognised_fallbacks.restype = None
    _handle.vterm_screen_set_unrecognised_fallbacks.argtypes = (c.POINTER(VTermScreen), c.POINTER(VTermStateFallbacks), c.POINTER(None))

    _handle.vterm_screen_get_unrecognised_fbdata.restype = c.POINTER(c.c_ubyte)
    _handle.vterm_screen_get_unrecognised_fbdata.argtypes = (c.POINTER(VTermScreen),)
    _handle.vterm_screen_get_unrecognised_fbdata.errcheck = lambda v,*a : c.cast(v, c.c_void_p)

    _handle.vterm_screen_enable_reflow.restype = None
    _handle.vterm_screen_enable_reflow.argtypes = (c.POINTER(VTermScreen), c.c_bool)

    _handle.vterm_screen_enable_altscreen.restype = None
    _handle.vterm_screen_enable_altscreen.argtypes = (c.POINTER(VTermScreen), c.c_int)

    _handle.vterm_screen_flush_damage.restype = None
    _handle.vterm_screen_flush_damage.argtypes = (c.POINTER(VTermScreen), )

    _handle.vterm_screen_set_damage_merge.restype = None
    _handle.vterm_screen_set_damage_merge.argtypes = (c.POINTER(VTermScreen), VTermDamageSize)

    _handle.vterm_screen_reset.restype = None
    _handle.vterm_screen_reset.argtypes = (c.POINTER(VTermScreen), c.c_int)

    _handle.vterm_screen_get_chars.restype = c.c_size_t
    _handle.vterm_screen_get_chars.argtypes = (c.POINTER(VTermScreen), c.POINTER(uint32_t), c.c_size_t, VTermRect)

    _handle.vterm_screen_get_text.restype = c.c_size_t
    _handle.vterm_screen_get_text.argtypes = (c.POINTER(VTermScreen), c.c_char_p, c.c_size_t, VTermRect)

    _handle.vterm_screen_get_attrs_extent.restype = c.c_int
    _handle.vterm_screen_get_attrs_extent.argtypes = (c.POINTER(VTermScreen), c.POINTER(VTermRect), VTermPos, VTermAttrMask)

    _handle.vterm_screen_get_cell.restype = c.c_int
    _handle.vterm_screen_get_cell.argtypes = (c.POINTER(VTermScreen), VTermPos, c.POINTER(VTermScreenCell))

    _handle.vterm_screen_is_eol.restype = c.c_int
    _handle.vterm_screen_is_eol.argtypes = (c.POINTER(VTermScreen), VTermPos, )

    _handle.vterm_screen_convert_color_to_rgb.restype = None
    _handle.vterm_screen_convert_color_to_rgb.argtypes = (c.POINTER(VTermScreen), c.POINTER(VTermColor), )

    _handle.vterm_screen_set_default_colors.restype = None
    _handle.vterm_screen_set_default_colors.argtypes = (c.POINTER(VTermScreen), c.POINTER(VTermColor), c.POINTER(VTermColor) )

    _handle.vterm_get_attr_type.restype = VTermValueType
    _handle.vterm_get_attr_type.argtypes = (VTermAttr, )

    _handle.vterm_get_prop_type.restype = VTermValueType
    _handle.vterm_get_prop_type.argtypes = (VTermProp, )

    _handle.vterm_scroll_rect.restype = None
    _handle.vterm_scroll_rect.argtypes = (VTermRect, c.c_int, c.c_int,
                                          c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, VTermRect, c.POINTER(None)),
                                          c.CFUNCTYPE(UNCHECKED(c.c_int), VTermRect, c.c_int, c.POINTER(None)), c.POINTER(None) )

    _handle.vterm_copy_cells.restype = None
    _handle.vterm_copy_cells.argtypes = (VTermRect, VTermRect, c.CFUNCTYPE(UNCHECKED(None),
                                                                           VTermPos, VTermPos, c.POINTER(None)), c.POINTER(None) )
    return _handle


if __name__ == "__main__":
    load_vterm("D:\\opt\\msys64\\ucrt64\\bin\\libvterm-0.dll")